<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="proname">
          <el-input v-model="filters.proname" placeholder="商品名称"></el-input>
        </el-form-item>
        <el-form-item prop="shopname">
          <el-input v-model="filters.shopname" placeholder="店铺名称"></el-input>
        </el-form-item>
        <el-form-item prop="number">
          <el-input v-model="filters.number" placeholder="SPU编码"></el-input>
        </el-form-item>
        <el-form-item prop="typename">
          <el-input v-model="filters.typename" placeholder="商品类目"></el-input>
        </el-form-item>
        <el-form-item prop="brandname">
          <el-input v-model="filters.brandname" placeholder="品牌"></el-input>
        </el-form-item>
        <el-form-item prop="status">
           <el-select v-model="filters.status" placeholder="上下架">
                <el-option
                v-for="item in statusList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="type">
           <el-select v-model="filters.type" placeholder="销售类型">
                <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="approvalstatus">
           <el-select v-model="filters.approvalstatus" placeholder="审核状态">
                <el-option
                v-for="item in approvalstatusList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-row>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
          </el-form-item>
          <el-form-item>
            <kt-button label="商品审核" type="primary" @click="editButton" />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      permsDelete="false"
      :data="pageResult"
      :columns="filterColumns"
      :showEditOperation="false"
      :showDetailOperation="true"
      :showBatchDelete="true"
      :showOperation="true"
      :showDeleteOperation="false"
      @findPage="findPage"
      @handleDetail="handleDetail"
      @selectionChange="selectionChange"
      ref="CyTable"
    ></cy-table>
    <el-dialog
      :title="'审核'"
      width="20%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="审核状态" prop="key">
           <el-select v-model="dataForm.approvalstatus" placeholder="审核状态">
                <el-option
                v-for="item in approvalstatusList2"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="editStatusForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      filters: {
        shopname: "",
        proname: "",
        number: "",
        typename: "",
        brandname: "",
        status: "",
        approvalstatus: "",
        type: "",
        t: "productApprove"
      },
      idArray:[],
      detailParam:{},
      typeList: [{'value':'','label':'请选择'},{'value':'零售','label':'零售'},{'value':'批发','label':'批发'}],
      statusList: [{'value':'','label':'请选择'},{'value':'0','label':'上架'},{'value':'1','label':'下架'}],
      approvalstatusList: [{'value':'','label':'请选择'},{'value':'0','label':'待审核'},{'value':'1','label':'审核通过'},{'value':'2','label':'审核拒绝'}],
      approvalstatusList2: [{'value':'1','label':'审核通过'},{'value':'2','label':'审核拒绝'}],
      columns: [
        { prop: "id", label: "编号", minWidth: 100},
        { prop: "pname", label: "商品名称", minWidth: 100},
        { prop: "number", label: "SPU编码", minWidth: 100 },
        { prop: "type", label: "销售类型", minWidth: 80 },
        { prop: "marketprice", label: "市场价/设价方式", minWidth: 120},
        { prop: "typename", label: "商品类目", minWidth: 120 },
        { prop: "brandname", label: "品牌", minWidth: 100 },
        { prop: "productstatus", label: "上下架", minWidth: 100},
        { prop: "shopname", label: "所属商家", minWidth: 100 },
        { prop: "apprpvalstatusval", label: "审核状态", minWidth: 100 }
	  ],
	  filterColumns:[],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},
      dataFormRules: {
        approvalstatus: [{ required: true, message: "请选择审核状态", trigger: "blur" }]
      },
      operation: false, // true:新增, false:编辑
      editDialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      // 新增编辑界面数据
      dataForm: {
        id: "",
        approvalstatus: "1"
      }
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPage(this.filters);
    },
    closeDetail: function(params) {
      this.content = "";
    },
    selectionChange(data){
      console.log(data);
      this.idArray = data.selections;
    },
    editButton:function(){
      if(this.idArray.length<1){
        this.$message({
            message: '请选择需要操作的数据',
            type: 'error',
            center:true,
            offset:250,
            duration:1000
        });
        return false;
      }
      if(this.idArray.length>1){
        this.$message({
            message: '请选择单条数据',
            type: 'error',
            center:true,
            offset:250,
            duration:1000
        });
        return false;
      }
      
      this.editDialogVisible = true;
      this.operation = true;
      this.dataForm.id = this.idArray[0].id;
      this.resetForm("dataForm");
        // this_.findPage(null);
    },
    editStatusForm:function(){
        console.log(this.dataForm.id);
        this.$refs.dataForm.validate(valid => {
            if (valid) {
            this.$confirm("是否确认审核？", "提示", {}).then(() => {
                let params = Object.assign({}, this.dataForm);

                params.t = "product";
                var this_ = this;
                this.utils.request.editInfo(params, function(res) {
                if (res.code == "0000") {
                    this_.$message({ message: "操作成功", type: "success" });

                    this_.$refs["dataForm"].resetFields();

                    this_.editDialogVisible = false;

                    this_.findPage(null);
                } else {
                    this_.$message({
                    message: "操作失败",
                    type: "error"
                    });
                }
                });
            });
            }
        });
    },
    // 显示新增界面
    handleAdd: function() {
      this.editDialogVisible = true;
      this.operation = true;
      this.resetForm("dataForm");
    },
    // 显示编辑界面
    handleEdit: function(params) {
      this.editDialogVisible = true;
      this.operation = false;
      this.$nextTick(function() {
        this.dataForm = Object.assign({}, params.row);
      });
    },
    // 显示编辑界面
    handleDetail: function(params) {
        this.detailParam.t = "product";
        this.detailParam.id = params.row.id;
        this.dialogVisible = true;
        let this_ = this;

        this.utils.request.queryUserInfo(this.detailParam, function(data) {
            
        });
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },

    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields();
        this.findPage(null);
      }
    },

    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "轮播图管理下载";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    }
  },
  mounted() {
    this.initColumns();
  }
};
</script>

<style scoped>
</style>